/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.heathrow.daoImpl2;

import cz.muni.fi.heathrow.dao.FlightDAO;
import cz.muni.fi.heathrow.entities.Destination;
import cz.muni.fi.heathrow.entities.Flight;
import java.util.Calendar;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Tomas Skopal
 */
@Repository
public class FlightDAOImpl implements FlightDAO
{

    @PersistenceContext
    private EntityManager manager;

    public FlightDAOImpl()
    {
    }

    @Override
    public void create(Flight flight)
    {
        if (flight == null)
            throw new IllegalArgumentException("Flight");
        if (flight.getId() != null)
            throw new IllegalArgumentException("Flights id is already set");

        manager.persist(flight);
    }

    @Override
    public Flight get(Long id)
    {
        if (id == null)
            throw new IllegalArgumentException("id");

        return manager.find(Flight.class, id);
    }

    @Override
    public void update(Flight flight)
    {
        if (flight == null)
            throw new IllegalArgumentException("Flight");
        if (flight.getId() == null)
            throw new IllegalArgumentException("Flights id is null");

        manager.merge(flight);
    }

    @Override
    public void remove(Flight flight)
    {
        if (flight == null)
            throw new IllegalArgumentException("Flight");
        if (flight.getId() == null)
            throw new IllegalArgumentException("Flights id is null");

        manager.remove(flight);
    }

    @Override
    public List<Flight> findAll()
    {
        TypedQuery<Flight> q = manager.createQuery("SELECT d FROM Flight AS d ORDER BY d.id DESC", Flight.class);
        return q.getResultList();
    }

    @Override
    public List<Flight> findByTakeoff(Calendar from, Calendar till)
    {
        if (from == null)
            throw new IllegalArgumentException("Date from is null");
        if (till == null)
            throw new IllegalArgumentException("Date till is null");

        TypedQuery<Flight> q = manager.createQuery("SELECT d FROM Flight AS d WHERE d.departure >= :from AND d.departure < :till ", Flight.class);
        q.setParameter("from", from);
        q.setParameter("till", till);
        return q.getResultList();
    }

    @Override
    public List<Flight> findByDestination(Destination destination)
    {
        if (destination == null)
            throw new IllegalArgumentException("Destination is null");
        if (destination.getId() == null)
            throw new IllegalArgumentException("Destinations id is null");

        TypedQuery<Flight> q = manager.createQuery("SELECT d FROM Flight AS d WHERE d.flightTo = :destination", Flight.class);
        q.setParameter("destination", destination);
        return q.getResultList();
    }
}
